
<!DOCTYPE html>
<html lang="en" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>js数组的处理方法 - 陌闻</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="Fechin,"> 
    <meta name="description" content="数组的去重1.es6的新属性 filter()过滤123456789let arr=[2,4,5,2,4,3];//三个值分别是代表:当前数组的每一项、数组的下标、当前数组let change=ar,"> 
    <meta name="author" content="陌闻"> 
    <link rel="alternative" href="atom.xml" title="陌闻" type="application/atom+xml"> 
    <link rel="icon" href="/myblog/img/favicon.png"> 
    
    
<link rel="stylesheet" href="/myblog/css/diaspora.css">

<meta name="generator" content="Hexo 5.4.0"></head>

<body class="loading">
    <span id="config-title" style="display:none">陌闻</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="iconfont icon-home image-icon" href="javascript:;" data-url="http://strange-news.gitee.io/myblog"></a>
    <div title="播放/暂停" class="iconfont icon-play"></div>
    <h3 class="subtitle">js数组的处理方法</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="iconfont icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">js数组的处理方法</h1>
        <div class="stuff">
            <span>四月 15, 2021</span>
            
  <ul class="post-tags-list" itemprop="keywords"><li class="post-tags-list-item"><a class="post-tags-list-link" href="/myblog/tags/js/" rel="tag">js</a></li></ul>


        </div>
        <div class="content markdown">
            <h3 id="数组的去重"><a href="#数组的去重" class="headerlink" title="数组的去重"></a>数组的去重</h3><h4 id="1-es6的新属性-filter-过滤"><a href="#1-es6的新属性-filter-过滤" class="headerlink" title="1.es6的新属性 filter()过滤"></a>1.es6的新属性 filter()过滤</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">let</span> arr=[2,4,5,2,4,3];</span><br><span class="line">//三个值分别是代表:当前数组的每一项、数组的下标、当前数组</span><br><span class="line"><span class="built_in">let</span> change=arr.filter((element, index, self)=&gt;&#123;</span><br><span class="line">	//indexOf()总是返回第一个元素的位置</span><br><span class="line">	//后续的重复元素位置与indexOf()返回的位置不相等，返回<span class="literal">false</span></span><br><span class="line">	<span class="built_in">return</span> self.indexOf(element)===index;</span><br><span class="line">&#125;);</span><br><span class="line">console.log(change);</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="2-利用数组方法splice-删除重复元素"><a href="#2-利用数组方法splice-删除重复元素" class="headerlink" title="2.利用数组方法splice()删除重复元素"></a>2.利用数组方法splice()删除重复元素</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">let</span> arr = [2,4,5,2,4,3];</span><br><span class="line">   <span class="keyword">function</span> change(arr) &#123;</span><br><span class="line">       <span class="keyword">for</span>(<span class="built_in">let</span> i = 0; i &lt; arr.length-1; i++)&#123;</span><br><span class="line">           <span class="keyword">for</span>(<span class="built_in">let</span> j = i+1; j &lt; arr.length; j++)&#123;</span><br><span class="line">               <span class="keyword">if</span>(arr[i]==arr[j])&#123;</span><br><span class="line">                   arr.splice(j,1); </span><br><span class="line">                   j--;</span><br><span class="line">               &#125;</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="built_in">return</span> arr;</span><br><span class="line">   &#125;</span><br><span class="line">   <span class="built_in">let</span> arr2 = change(arr);</span><br><span class="line">   console.log(arr2);</span><br></pre></td></tr></table></figure>

<h4 id="3-利用空对象来记录新数组中已经存储过的元素"><a href="#3-利用空对象来记录新数组中已经存储过的元素" class="headerlink" title="3.利用空对象来记录新数组中已经存储过的元素"></a>3.利用空对象来记录新数组中已经存储过的元素</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">let</span> arr = [2,4,5,2,4,3];</span><br><span class="line">    console.log(arr);</span><br><span class="line">    <span class="built_in">let</span> obj=&#123;&#125;;</span><br><span class="line">    <span class="built_in">let</span> arr2=[];</span><br><span class="line">    <span class="keyword">for</span>(<span class="built_in">let</span> i=0;i&lt;arr.length;i++)&#123;</span><br><span class="line">        <span class="keyword">if</span>(!obj[arr[i]])&#123;</span><br><span class="line">            obj[arr[i]]=<span class="literal">true</span>;</span><br><span class="line">            arr2.push(arr[i]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    console.log(arr2); </span><br></pre></td></tr></table></figure>

<h3 id="数组的最大、最小值"><a href="#数组的最大、最小值" class="headerlink" title="数组的最大、最小值"></a>数组的最大、最小值</h3><h4 id="1-排序sort"><a href="#1-排序sort" class="headerlink" title="1.排序sort()"></a>1.排序sort()</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">let</span> arr=[3,2,7,8,10];</span><br><span class="line">//a-b是升序</span><br><span class="line">arr.sort((a-b)=&gt;&#123;</span><br><span class="line">	<span class="built_in">return</span> a-b;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="built_in">let</span> min=arr[0];</span><br><span class="line"><span class="built_in">let</span> max=arr[arr.length-1];</span><br></pre></td></tr></table></figure>
<h4 id="2-使用Math-min和Math-max"><a href="#2-使用Math-min和Math-max" class="headerlink" title="2.使用Math.min和Math.max"></a>2.使用Math.min和Math.max</h4><h5 id="1-Math-min和Math-max的参数只能是数字或字符串类型的数字，所以使用-apply-，为Math-min和Math-max传参数-arr"><a href="#1-Math-min和Math-max的参数只能是数字或字符串类型的数字，所以使用-apply-，为Math-min和Math-max传参数-arr" class="headerlink" title="(1)Math.min和Math.max的参数只能是数字或字符串类型的数字，所以使用 apply()，为Math.min和Math.max传参数 arr"></a>(1)Math.min和Math.max的参数只能是数字或字符串类型的数字，所以使用 apply()，为Math.min和Math.max传参数 arr</h5><h5 id="2-也可以通过赋值结构方式传参"><a href="#2-也可以通过赋值结构方式传参" class="headerlink" title="(2)也可以通过赋值结构方式传参"></a>(2)也可以通过赋值结构方式传参</h5> <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">let</span> arr=[3,2,7,8,10];</span><br><span class="line"><span class="built_in">let</span> max = Math.max.apply(null, arr);</span><br><span class="line"><span class="built_in">let</span> min = Math.min.apply(null, arr);</span><br><span class="line">console.log(max, min);</span><br><span class="line">//通过赋值结构方式传参</span><br><span class="line"><span class="built_in">let</span> arr = [3,2,7,8,10]; </span><br><span class="line">console.log(Math.max(...arr));</span><br><span class="line">console.log(Math.min(...arr));</span><br></pre></td></tr></table></figure>
            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls" data-autoplay="false">
                <source type="audio/mpeg" src="">
            </audio>
            
                <ul id="audio-list" style="display:none">
                    
                        
                            <li title='0' data-url='http://link.hhtjim.com/163/425570952.mp3'></li>
                        
                    
                        
                            <li title='1' data-url='http://link.hhtjim.com/163/425570952.mp3'></li>
                        
                    
                </ul>
            
        </div>
        
    <div id='gitalk-container' class="comment link"
		data-enable='false'
        data-ae='false'
        data-ci=''
        data-cs=''
        data-r='myblog'
        data-o='strange-news'
        data-a='strange-news'
        data-d='false'
    >查看评论</div>


    </div>
    
</div>


    </div>
</div>
<script src="/myblog/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/myblog/live2dw/assets/kesyoban.model.json"},"display":{"position":"right","width":200,"height":300,"opacity":0.8},"mobile":{"show":true,"scale":0.5},"log":false});</script></body>


<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="/myblog/js/plugin.js"></script>
<script src="/myblog/js/typed.js"></script>
<script src="/myblog/js/diaspora.js"></script>


<link rel="stylesheet" href="/myblog/photoswipe/photoswipe.css">
<link rel="stylesheet" href="/myblog/photoswipe/default-skin/default-skin.css">


<script src="/myblog/photoswipe/photoswipe.min.js"></script>
<script src="/myblog/photoswipe/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>






</html>
